package com.lwj.service;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.github.pagehelper.PageHelper;
import com.lwj.config.SpringThreadPoolConfig;
import com.lwj.excel.TestExcel;
import com.lwj.exception.BusinessException;
import com.lwj.mapper.administration.DepartmentMapper;
import com.lwj.mapper.administration.UserMapper;
import com.lwj.model.administration.User;
import com.lwj.service.administration.DepartmentService;
import com.lwj.service.common.AsyncTaskExecutorService;
import com.lwj.utils.excel.EasyExcelUtil;
import com.lwj.utils.excel.EasyExcelUtils;
import com.lwj.utils.excel.WaterMarkStrategy;
import com.lwj.vo.administration.DepartmentTreeVo;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.awt.print.Pageable;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class HelloServiceImpl implements HelloService{
    private static final Logger logger = LoggerFactory.getLogger(HelloServiceImpl.class);

    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private AsyncTaskExecutorService asyncTaskExecutorService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DepartmentService departmentService;

    @Override
    public String text(){
        logger.info("asyncTaskExecutor=="+Thread.currentThread());
        return "test";
    }
    @Async
    @Override
    public String textAsync() throws InterruptedException {
        logger.info("默认Executor=="+Thread.currentThread());
        return "test";
    }
    @Async("asyncTaskExecutor")//需要返回值或捕捉方法异常需要返回Future,并且执行它的get方法
    @Override
    public Future<String> text1() throws InterruptedException {

        return new AsyncResult("test");
    }
    @Override
    public String text2() {
        Callable<String> callable1=new Callable() {
            @Override
            public String call() throws Exception {
                logger.info(Thread.currentThread().getName());
                Thread.sleep(5000);
                return Thread.currentThread().getName();
            }
        };
        Callable<String> callable2=new Callable() {
            @Override
            public String call() throws Exception {
                logger.info(Thread.currentThread().getName());
                Thread.sleep(5000);
                return Thread.currentThread().getName();
            }
        };
        Callable<String> callable3=new Callable() {
            @Override
            public String call() throws Exception {
                logger.info(Thread.currentThread().getName());
                Thread.sleep(5000);
                return Thread.currentThread().getName();
            }
        };
        List<Callable<String> > task=new ArrayList<>();
        task.add(callable1);
        task.add(callable2);
        task.add(callable3);
        List<String> callable = asyncTaskExecutorService.callable(task);
        System.out.println(callable);
        return "test";
    }

    @Override
    public String text3(HttpServletResponse response) {
        EasyExcelUtil easyExcelUtils=EasyExcelUtil.defaultEasyExcelUtil("水印");
        String fileName="test.xlsx";
        String sheetName="test";
        sheetName=easyExcelUtils.replaceSheetName(sheetName);
        easyExcelUtils.addWriteHandler(TestExcel.writeHandler());
        try {
            ExcelWriter writer = easyExcelUtils.builder(fileName,response).head(TestExcel.class).build();
            WriteSheet writeSheet = new WriteSheet();
            writeSheet.setSheetName(sheetName);
            int pageNum=1;
            int pageSize=10;
            List<TestExcel> list=new ArrayList<>();
            do{
                Future<List<TestExcel>> data = departmentService.data(pageNum, pageSize);
                logger.error("写数据==="+new Date());
                writer.write(list, writeSheet);
//                Thread.sleep(3000);
                list= data.get();
                pageNum++;
            } while (!CollectionUtils.isEmpty(list));
            writer.finish();
        } catch (Exception e) {
            logger.error("导出报错", e);
            throw new BusinessException("导出报错");
        }
        return null;
    }

}
